package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Blog;
import com.etc.entity.BlogComnent;
import com.etc.entity.BlogName;
import com.etc.service.BlogService;
import com.etc.utils.CommonMessage;
import com.etc.utils.PageData;

@Controller
@RequestMapping(value = "adminapi")
public class ABlogController {

	@Resource(name = "blogService")
	private BlogService blogService;

	@RequestMapping(value = "toblog")
	public String toBlogList() {

		return "admin/blog-list";
	}

	@RequestMapping(value = "toblogpicture")
	public String toBlogPictureList() {

		return "admin/blogpicture-list";
	}

	@RequestMapping(value = "toblogadd")
	public String toBlogadd() {

		return "admin/blog-add";
	}

	@RequestMapping(value = "blogCount")
	public String selectCount(String content, Model model) {
		if (content == null) {
			content = "";
		}
		int a = blogService.selectCount(content);
		model.addAttribute("count", a);
		return "admin/blog-list";

	}

	/**
	 * 查询所有
	 * 
	 * @param page
	 * @param pagesize
	 * @param content
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "listBlog")
	public String selectAllBlog(Integer page, Integer pagesize, String content, Model model) {
		if (content == null) {
			content = "";
		}

		PageData<BlogName> pd = blogService.selectAllBlog(1, 20, content);
		List<BlogName> list = pd.getData();
		model.addAttribute("list", list);

		return "admin/blog-list";

	}

	/**
	 * 查询图片
	 * 
	 * @param page
	 * @param pagesize
	 * @param content
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "listPhoto")
	public String selectPhoto(Integer page, Integer pagesize, String content, Model model) {
		if (content == null) {
			content = "";
		}

		PageData<Blog> pd = blogService.selectPhoto(1, 20, content);
		List<Blog> list = pd.getData();
		model.addAttribute("list", list);

		return "admin/blogpicture-list";

	}

	/**
	 * 启用博客
	 * 
	 * @param bid
	 * @return
	 */
	@RequestMapping(value = "blogUpdateState", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage updateByPrimaryKey(int bid) {

		boolean flag = blogService.updateByPrimaryKey(bid);
		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}
		return cm;
	}

	/**
	 * 禁用博客
	 * 
	 * @param bid
	 * @return
	 */
	@RequestMapping(value = "blogUpdateAudit", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage updateaudit(int bid) {

		boolean flag = blogService.updateAudit(bid);
		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}
		return cm;
	}

	/**
	 * 查询评论
	 * 
	 * @param bid
	 * @return
	 */

	@RequestMapping(value = "blogComment", method = RequestMethod.GET)
	public ModelAndView selectComment(int bid) {
		ModelAndView model = new ModelAndView();
		model.setViewName("admin/_blank");
		List<BlogComnent> bc = blogService.selectComment(bid);
		model.addObject("bc", bc);
		return model;

	}
	@RequestMapping(value="delComnent", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage deleteByPrimaryKey(int bcid) {
		boolean flag=blogService.deleteByPrimaryKey(bcid);
		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

	/**
	 * 增加博客
	 * 
	 * @param btitle
	 * @param uid
	 * @param boraise
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addblog")

	public String addRestaurant(String btitle, Integer uid, String boraise,
			@RequestParam(value = "myfile") MultipartFile file, HttpServletRequest request) {

		String str = file.getOriginalFilename();
		String[] arr = str.split("\\.");
		if (arr[arr.length - 1].equals("jpg") || arr[arr.length - 1].equals("png")) {
			// 获取上传路径
			String path = "D:\\imgs\\blog" + "\\" + file.getOriginalFilename();
			// 创建目标文件
			File destFile = new File(path);
			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String bimg = "/imgUrl/bolg/" + file.getOriginalFilename();
			Blog blog = new Blog(null, uid, btitle, boraise, 0, 1, bimg);
			boolean flag = blogService.addBlog(blog);
			if (flag) {
				return "redirect:listBlog/";
			} else {
				request.setAttribute("msg", "新增失败");
			}

			request.setAttribute("msg", "新增成功");
			return "redirect:listBlog/";
		} else {
			request.setAttribute("msg", "新增失败");
			return "admin/blog-add";
		}
	}

	/**
	 * 修改博客
	 * 
	 * @param bid
	 * @param name
	 * @param btitle
	 * @param bcontent
	 * @param boraise
	 * @param bstate
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "updateBlog01", method = RequestMethod.GET)
	public String updateblog01(@RequestParam Integer bid, @RequestParam String name, @RequestParam String btitle,
			@RequestParam String bcontent, @RequestParam Integer boraise, @RequestParam Integer bstate, Model model) {

		BlogName bn = new BlogName(bid, name, btitle, bcontent, boraise, bstate);

		model.addAttribute("bn", bn);
		return "admin/article-add";
	}

	@RequestMapping(value = "updateBlog", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage updateblog(@Param("bid") Integer bid, @Param("btitle") String btitle,
			@Param("bcontent") String bcontent, @Param("boraise") Integer boraise) {

		Blog blog = new Blog(bid, null, btitle, bcontent, boraise, null, null);
		boolean flag = blogService.updateBlog(blog);

		CommonMessage cm = new CommonMessage();

		if (flag) {
			cm.setMsg("操作成功");
		} else {
			cm.setMsg("操作失败");
		}

		return cm;
	}

}
